package com.gd.data.platform.controller.api;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gd.data.platform.dto.ApiResponse;
import com.gd.data.platform.dto.OfflineAnalysisResult;
import com.gd.data.platform.service.offline.OfflineAnalysisService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 离线分析API控制器
 * 提供离线数据分析接口
 * 
 * @author gd-data-platform
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/offline")
@Api(tags = "离线分析API")
public class OfflineAnalysisController {

    @Autowired
    private OfflineAnalysisService offlineAnalysisService;

    /**
     * 手动执行离线分析
     */
    @PostMapping("/analyze")
    @ApiOperation("手动执行离线分析")
    public ApiResponse<OfflineAnalysisResult> executeOfflineAnalysis(
            @ApiParam("开始时间") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam("结束时间") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            OfflineAnalysisResult result = offlineAnalysisService.executeOfflineAnalysis(startTime, endTime);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("执行离线分析失败", e);
            return ApiResponse.error("执行离线分析失败：" + e.getMessage());
        }
    }

    /**
     * 获取分析结果
     */
    @GetMapping("/results")
    @ApiOperation("获取分析结果")
    public ApiResponse<String> getAnalysisResults() {
        try {
            // 这里需要实现获取分析结果的逻辑
            return ApiResponse.success("分析结果获取成功");
        } catch (Exception e) {
            log.error("获取分析结果失败", e);
            return ApiResponse.error("获取分析结果失败：" + e.getMessage());
        }
    }
}
